package com.wyx.videoplay.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.wyx.videoplay.AddCustormActivity;
import com.wyx.videoplay.R;
import com.wyx.videoplay.adapter.Book;
import com.wyx.videoplay.adapter.BookAdapter;
import com.wyx.videoplay.itemdecoration.SimplePaddingDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import drawthink.expandablerecyclerview.bean.RecyclerViewData;
import drawthink.expandablerecyclerview.listener.OnRecyclerViewListener;


/**
 * A simple {@link Fragment} subclass.
 */
public class CustomFragment extends Fragment  implements OnRecyclerViewListener.OnItemClickListener, OnRecyclerViewListener.OnItemLongClickListener{

    private static final String TAG = "wuyunxiang";
    private List<RecyclerViewData> mDatas=new ArrayList<>();
    private BookAdapter adapter;
    private LinearLayoutManager linearLayoutManager;
    private RecyclerView rvCompany;
    private TextView tvRightText;
    private TextView tvTitle;

    public static CustomFragment newInstance() {
        CustomFragment fragment = new CustomFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_custom_info, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        rvCompany = view.findViewById(R.id.rv_company);

        view.findViewById(R.id.iv_back_layout_top).setVisibility(View.INVISIBLE);
        tvRightText = view.findViewById(R.id.iv_option2_layout_top);
        tvTitle = view.findViewById(R.id.tv_title_layout_top);
        tvTitle.setText("客户信息");
        tvRightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), AddCustormActivity.class));
            }
        });

        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvCompany.addItemDecoration(new SimplePaddingDecoration(getActivity()));
        initBooks();
        rvCompany.setLayoutManager(linearLayoutManager);
        adapter = new BookAdapter(getActivity(), mDatas);
        adapter.setOnItemClickListener(this);
        adapter.setOnItemLongClickListener(this);

        rvCompany.setAdapter(adapter);

        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void initBooks() {

        List<Book> bean2 = new ArrayList<>();
        List<Book> bean3 = new ArrayList<>();
        List<Book> bean4 = new ArrayList<>();
        // id , pid , label , 其他属性
        List<Book> bean1 = new ArrayList<>();
        bean1.add(new Book("文件管理系统"));
        bean1.add(new Book("游戏"));
        bean1.add(new Book("文档"));
        bean1.add(new Book("程序"));
        bean1.add(new Book("面向对象"));
        bean2.add(new Book("war3"));
        bean2.add(new Book("刀塔传奇"));


        bean2.add(new Book("非面向对象"));

        bean2.add(new Book("C++"));
        bean2.add(new Book("JAVA"));
        bean2.add(new Book("Javascript"));
        bean2.add(new Book("C"));

        bean3.add(new Book("文件管理系统"));
        bean3.add(new Book("游戏"));
        bean4.add(new Book("文档"));
        bean4.add(new Book("程序"));
        bean4.add(new Book("war3"));
        bean3.add(new Book("刀塔传奇"));

        bean3.add(new Book("面向对象"));
        bean4.add(new Book("非面向对象"));

        bean3.add(new Book("文件管理系统"));
        bean3.add(new Book("游戏"));
        bean4.add(new Book("文档"));
        bean4.add(new Book("程序"));
        bean4.add(new Book("war3"));
        bean4.add(new Book("刀塔传奇"));

        mDatas.add(new RecyclerViewData("分组0", new ArrayList(), false));
        mDatas.add(new RecyclerViewData("分组1", bean2, false));
        mDatas.add(new RecyclerViewData("分组2", bean3, false));
        mDatas.add(new RecyclerViewData("分组3", bean4, false));
    }

    @Override
    public void onGroupItemClick(int position,int groupPosition, View view) {
        String group = (String) mDatas.get(groupPosition).getGroupData();
        Log.e(TAG,"groupPos:" + groupPosition + " group:" + group);
//        Toast.makeText(getActivity(), "groupPos:" + groupPosition + " group:" + group, Toast.LENGTH_SHORT).show();
//        mDatas.get(groupPosition).getGroupData()
        if(mDatas.get(groupPosition).getGroupItem().getChildDatas().size()==0){
            List<Book> bean1 = new ArrayList<>();
            bean1.add(new Book("文件管理系统"));
            bean1.add(new Book("游戏"));
            bean1.add(new Book("文档"));
            bean1.add(new Book("程序"));
            bean1.add(new Book("面向对象"));
            mDatas.get(groupPosition).getGroupItem().setChildDatas(bean1);
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onChildItemClick(int position,int groupPosition, int childPosition, View view) {
        Book bean = (Book) mDatas.get(groupPosition).getChild(childPosition);
        Log.e(TAG,"groupPos:" + groupPosition + "  childPos:" + childPosition + " child:" + bean.getName());
//        Toast.makeText(getActivity(), "groupPos:" + groupPosition + "  childPos:" + childPosition + " child:" + bean.getName(), Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onGroupItemLongClick(int position,int groupPosition, View view) {
//        String group = (String) mDatas.get(groupPosition).getGroupData();
//        Toast.makeText(getActivity(), "groupPos:" + groupPosition + " group:" + group, Toast.LENGTH_SHORT).show();
//        showDeleteDialog(position,groupPosition,0,true);
    }

    @Override
    public void onChildItemLongClick(int position,int groupPosition, int childPosition, View view) {
        Book bean = (Book) mDatas.get(groupPosition).getChild(childPosition);
        Toast.makeText(getActivity(), "groupPos:" + groupPosition + "  childPos:" + childPosition + " child:" + bean.getName(), Toast.LENGTH_SHORT).show();
        showDeleteDialog(position,groupPosition,childPosition,false);
    }

    /**
     * 删除数据
     * @param position
     * @param isGroup
     */
    private void showDeleteDialog(final int position, final int groupPosition, final int childPosition, final boolean isGroup){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
                .setTitle("提示!")
                .setMessage(isGroup?"您确定要删除此组数据":"您确定要删除此条数据")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //然后根据回调里的groupPosition和childPosition来更新你的数据源
                        if(isGroup){
                            mDatas.remove(groupPosition);
                        }else {
                            mDatas.get(groupPosition).removeChild(childPosition);
                        }
                        adapter.notifyRecyclerViewData();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.show();
    }

}
